package org.deri.odhd.buses.util;

import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.deri.odhd.buses.model.Journey;
import org.deri.odhd.buses.model.Stop;

public class JsonUtil {

	public static ObjectNode jsonize(List<Journey> journeysList){
		ObjectMapper mapper = new ObjectMapper();
		ObjectNode resultObj = mapper.createObjectNode();
		ArrayNode resultsArr = mapper.createArrayNode();
		
		for(Journey journey:journeysList){
			ObjectNode resultItemObj = mapper.createObjectNode();
			resultItemObj.put("companyName", journey.getCompany().getName());
			resultItemObj.put("day", journey.getDay());
			ArrayNode stopsArr = mapper.createArrayNode();
			if(journey.getStops()!=null){
				for(Stop s:journey.getStops()){
					ObjectNode stopObj = mapper.createObjectNode();
					stopObj.put("name", s.getName());
					stopObj.put("time", s.getTime().toString());
					stopsArr.add(stopObj);
				}
			}
			resultItemObj.put("stops", stopsArr);
			
			//add to array
			resultsArr.add(resultItemObj);
		}
		resultObj.put("results", resultsArr);
		
		return resultObj;
	}
}
